package com.atendimento

class LocalController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [localInstanceList: Local.list(params), localInstanceTotal: Local.count()]
    }

    def create = {
        def localInstance = new Local()
        localInstance.properties = params
        return [localInstance: localInstance]
    }

    def save = {
        def localInstance = new Local(params)
        if (!localInstance.hasErrors() && localInstance.save()) {
            flash.message = "local.created"
            flash.args = [localInstance.id]
            flash.defaultMessage = "Local ${localInstance.id} created"
            redirect(action: "edit", id: localInstance.id)
        }
        else {
            render(view: "edit", model: [localInstance: localInstance])
        }
    }

    def show = {
        def localInstance = Local.get(params.id)
        if (!localInstance) {
            flash.message = "local.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Local not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [localInstance: localInstance]
        }
    }

    def edit = {
        def localInstance = Local.get(params.id)
        if (!localInstance) {
            flash.message = "local.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Local not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [localInstance: localInstance]
        }
    }

    def update = {
        def localInstance = Local.get(params.id)
        if (localInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (localInstance.version > version) {
                    
                    localInstance.errors.rejectValue("version", "local.optimistic.locking.failure", "Another user has updated this Local while you were editing")
                    render(view: "edit", model: [localInstance: localInstance])
                    return
                }
            }
            localInstance.properties = params
            if (!localInstance.hasErrors() && localInstance.save()) {
                flash.message = "local.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Local ${params.id} updated"
                redirect(action: "edit", id: localInstance.id)
            }
            else {
                render(view: "edit", model: [localInstance: localInstance])
            }
        }
        else {
            flash.message = "local.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Local not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def localInstance = Local.get(params.id)
        if (localInstance) {
            try {
                localInstance.delete()
                flash.message = "local.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Local ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "local.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Local ${params.id} could not be deleted"
                redirect(action: "edit", id: params.id)
            }
        }
        else {
            flash.message = "local.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Local not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
